package com.stox.charting.ui.swing.model.price;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Date;

import com.stox.charting.ui.swing.model.Axis;
import com.stox.charting.ui.swing.model.ChartModel;
import com.stox.charting.ui.swing.model.Orientation;
import com.stox.charting.ui.swing.model.Theme;
import com.stox.charting.ui.swing.model.uiUnit.UiUnit;
import com.stox.core.model.IBar;
import com.stox.core.util.DateUtil;

public class DateAxis extends Axis {
	private static final long serialVersionUID = -7035140695806503193L;

	private ChartModel<? extends IBar> chartModel;
	private static final int markHeight = 5;
	private static final int textHorizontalGap = 10;
	private static final int textVerticalGap = 3;
	
	private PriceChartView priceChartView;
	
	public DateAxis(PriceChartView chartView, ChartModel<? extends IBar> chartModel) {
		super(chartView, Orientation.HORIZONTAL, BorderLayout.SOUTH);
		setBackground(Color.blue);
		this.chartModel = chartModel;
		this.priceChartView = chartView;
	}
	
	@SuppressWarnings("deprecation")
	@Override
	protected synchronized void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		int prevMonth = -1;
		int prevYear = -1;
		int prevDate = -1;
		Rectangle r = getVisibleRect();
		int lastTextX = Integer.MIN_VALUE;
		g.setFont(Theme.getFonts().getSmallFont());
		priceChartView.getGrid().getVerticalLines().clear();
		for(int index = chartModel.getViewport().getStartIndex()-1; index >= chartModel.getViewport().getEndIndex(); index--){
			
			UiUnit uiUnit = chartModel.getViewport().getUiUnits().get(index);
			int x = (int)(uiUnit.getX()+ uiUnit.getWidth()/2 + Theme.getStructure().getVerticalAxisWidth());
			
			g.setColor(Theme.getColours().getAxisLine());
			g.drawLine(x, (int)r.getY(), x, (int)r.getY()+ markHeight);
			
			Date date = chartModel.getData().get(index).getDate();
			
			int month = date.getMonth();
			int year = date.getYear();
			int intDate = date.getDate();
			String text = null;
			
			if(prevYear != year){
				text = String.valueOf(date.getYear()+1900);
			}else if(prevMonth != month){
				text = DateUtil.getTextMonth(month);
			}else if(prevDate != intDate){
				text = String.valueOf(date.getDate());
			}
			
			if(text != null){
				int textWidth = (int)g.getFont().getStringBounds(text, ((Graphics2D)g).getFontRenderContext()).getWidth();
				
				int nextTextX = lastTextX + textWidth;
				
				if(x > lastTextX + textHorizontalGap && nextTextX <= r.getX()+r.getWidth()){
					prevMonth = month;
					prevYear = year;
					prevDate = intDate;
					g.setColor(Theme.getColours().getAxisText());
					
					int textHeight = (int)g.getFont().getStringBounds(text, ((Graphics2D)g).getFontRenderContext()).getHeight();
					g.setColor(Theme.getColours().getAxisText());
					g.drawString(text, x, (int)r.getY()+ markHeight + textHeight + textVerticalGap);
					priceChartView.getGrid().getVerticalLines().add((int)(uiUnit.getX()+ uiUnit.getWidth()/2));
					
					lastTextX = x + textWidth;
				}
			}
			
		}
		
	}

}
